package com.zrrd.yunchmall.sale.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.yunchmall.sale.entity.FlashPromotionSession;
import com.zrrd.yunchmall.sale.service.IFlashPromotionSessionService;
import com.zrrd.yunchmall.user.entity.Admin;
import com.zrrd.yunchmall.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 限时购场次表 前端控制器
 * </p>
 *
 * @author LYY
 * @since 2024-01-15
 */
@RestController
@RequestMapping("/flashSession")
@Api(tags = "营销服务-商品模块")
public class FlashPromotionSessionController {
    @Autowired
    private IFlashPromotionSessionService flashPromotionSessionService;

    @ApiOperation("设置商品")
    @GetMapping("/selectList")
    public ResponseResult selectList(@RequestParam int flashPromotionId) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id", flashPromotionId);


        return new ResponseResult<>(200, "查询成功!", queryWrapper);
    }

    @ApiOperation("添加秒杀时间段")
    @GetMapping("/list")
    public ResponseResult list(@RequestParam Long flashPromotionId) {
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("flash_promotion_id", flashPromotionId);
        queryWrapper.select("flash_promotion_session_id");
        List<FlashPromotionSession> flashPromotionSessionList = flashPromotionSessionService.list(queryWrapper);
        flashPromotionSessionList.forEach(flashPromotionSession -> {
            queryWrapper.clear();
            queryWrapper.eq("flash_promotion_id", flashPromotionId);
            queryWrapper.eq("flash_promotion_session_id", flashPromotionSession.getId());
            Long count = flashPromotionSession.getProductCount();
            flashPromotionSession.setProductCount(count);
        });
        return new ResponseResult<>(200, "查询成功!", flashPromotionSessionList);
    }
}

